package com.xiaoka.ins.corp.api.gpic.help;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.xiaoka.ins.corp.api.utils.BaseUtils;

/**
 * 
 * @author zhanghuang@sinosoft.com.cn
 * 
 */
public class GpicUtils {
	private static final String ALGORITHM = "RSA";
	private static final String SIGNPARAMTER = "MD5WithRSA";
   //加密
	public static String sign(byte[] data, PrivateKey priKey) {
		try {
			Signature signature = Signature.getInstance(SIGNPARAMTER);
			signature.initSign(priKey);
			signature.update(data);
			return Base64.encodeBase64URLSafeString(signature.sign());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    //解密
	public static boolean verify(byte[] data, String sign, PublicKey pubKey) {
		try {
			Signature signature = Signature.getInstance(SIGNPARAMTER);
			signature.initVerify(pubKey);
			String source = new String(data);
			int pos = source.indexOf("?");
			if(pos ==0){
				source = source.replaceFirst("\\?", "");
			}
			signature.update(source.getBytes());
			return signature.verify(Base64.decodeBase64(sign));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static PrivateKey getPrivateKey(String key) {
		try {
		  byte[] keyBytes = (Base64.decodeBase64(key));
		  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		  KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
		  PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		  return privateKey;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static PublicKey getPublicKey(String key) {
	    try {
	      byte[] keyBytes = (Base64.decodeBase64(key));
	      X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
	      KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
	      PublicKey publicKey = keyFactory.generatePublic(keySpec);
	      return publicKey;
	    } catch (Exception e) {
	      e.printStackTrace();
	      return null;
	    }
	}
	
	//生成秘钥对
	private byte[] pubKey;
	private byte[] priKey;

	public byte[] getPubKey() {
		return pubKey;
	}
	public void setPubKey(byte[] pubKey) {
		this.pubKey = pubKey;
	}
	public byte[] getPriKey() {
		return priKey;
	}
	public void setPriKey(byte[] priKey) {
		this.priKey = priKey;
	}
	
	public static String filterRequestXml(String xml) {
		xml = xml.replace("<?xml version=\"1.0\" encoding=\"GBK\" standalone=\"yes\"?>","");
		return xml.replace(BaseUtils.getBetweenStr(xml, "<Request", ">"), "");
	}
	
	public static String filterPackageListXml(String xml) {
		xml = xml.replace(BaseUtils.getBetweenStr(xml, "<PackageList", ">"), "");
		xml = xml.replace(BaseUtils.getBetweenStr(xml, "XMLSchema-instance\"", ">"), "");
		return xml.replace("<br>", "。");
	}
	
}
